This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
require(dplyr)
Carregando pacotes exigidos: dplyr
Attaching package: 㤼㸱dplyr㤼㸲
The following objects are masked from 㤼㸱package:stats㤼㸲:
filter, lag
The following objects are masked from 㤼㸱package:base㤼㸲:
intersect, setdiff, setequal, union
require(igraph)
Carregando pacotes exigidos: igraph
Attaching package: 㤼㸱igraph㤼㸲
The following objects are masked from 㤼㸱package:dplyr㤼㸲:
as_data_frame, groups, union
The following objects are masked from 㤼㸱package:stats㤼㸲:
decompose, spectrum
The following object is masked from 㤼㸱package:base㤼㸲:
union
# Load Hospitalization Data
flows<-read.csv(url('https://s3-sa-east-1.amazonaws.com/ckan.saude.gov.br/SRAG/2021/INFLUD21-05-07-2021.csv'))%>%
filter()
# Load Hospitalization Data
flows<-read.table('Z:/Master/Datasus/raw/SRAG/influd21-18-06-2021.csv',sep=';',header=T,encoding='UTF-8')%>%
filter(SG_UF_INTE=='SP'&SG_UF=='SP')%>%
mutate(date=as.Date(DT_INTERNA,format='%D/%M/%Y'),
age=NU_IDADE_N)
flows_count<-flows%>%
filter(!is.na(CO_MUN_RES)&!is.na(CO_MU_INTE))%>%
group_by(mun_res=CO_MUN_RES,mun_hosp=CO_MU_INTE)%>%
summarize(count=n(),count_icu=sum(ifelse(UTI==1,1,0)))%>%
mutate(share=count/sum(count),share_icu=count_icu/sum(count_icu,na.rm=T))
`summarise()` regrouping output by 'mun_res' (override with `.groups` argument)
# Load Shapefile
require(geobr)
Carregando pacotes exigidos: geobr
require(sf)
Carregando pacotes exigidos: sf
Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
shp<-read_municipality(year=2020)%>%
filter(abbrev_state=='SP')%>%
mutate(mun=floor(code_muni/10))%>%
select(mun,name_muni,geom)
Using year 2020
Loading data for the whole country. This might take a few minutes.
|
| | 0%
|
|===== | 4%
|
|========== | 7%
|
|=============== | 11%
|
|==================== | 15%
|
|======================== | 19%
|
|============================= | 22%
|
|================================== | 26%
|
|======================================= | 30%
|
|============================================ | 33%
|
|================================================= | 37%
|
|====================================================== | 41%
|
|=========================================================== | 44%
|
|================================================================ | 48%
|
|==================================================================== | 52%
|
|========================================================================= | 56%
|
|============================================================================== | 59%
|
|=================================================================================== | 63%
|
|======================================================================================== | 67%
|
|============================================================================================= | 70%
|
|================================================================================================== | 74%
|
|======================================================================================================= | 78%
|
|============================================================================================================ | 81%
|
|================================================================================================================ | 85%
|
|===================================================================================================================== | 89%
|
|========================================================================================================================== | 93%
|
|=============================================================================================================================== | 96%
|
|====================================================================================================================================| 100%
shp_centroid<-shp%>%
group_by(mun)%>%
mutate(centr=st_centroid(geom))
require(ggplot2)
require(plotly)
# Network
nodes<-flows_count%>%
data.table::melt(measure.vars=c('mun_res','mun_hosp'))%>%
distinct(value)%>%
dplyr::rename(mun=value)
edges<-nodes%>%rename(mun_res=mun)%>%
merge(nodes%>%rename(mun_hosp=mun),all=T)%>%
left_join(flows_count,by=c('mun_res','mun_hosp'))%>%
mutate_at(.vars=c('share','share_icu'),.funs=function(x) {ifelse(is.na(x),0,x)})%>%
rename(weight=share)
dir_net<-graph_from_data_frame(edges%>%rename(),nodes,directed=TRUE)
nodes_cluster<-nodes%>%
mutate(cluster_fg=cluster_fast_greedy(as.undirected(dir_net))$membership,
label=reorder(paste0('Cluster: ',cluster_fg),cluster_fg))
# Map
gph=ggplot(nodes_cluster%>%
inner_join(shp,by=c('mun')),aes(text=name_muni))+
geom_sf(aes(geometry=geom,fill=label),lwd=0.1,show.legend=F)+
geom_sf(data=shp_centroid%>%filter(name_muni=='São Paulo')%>%ungroup,aes(geometry=centr))+
theme_void()+
theme(axis.line=element_blank())+
scale_fill_manual(values=rep(RColorBrewer::brewer.pal(n=12,name='Paired'),times=10))+
guides(fill=guide_legend(ncol=3))+
labs(caption='Note: Check cluster by hovering over each municipality')
plotly::ggplotly(gph,tooltip=c('text','fill'))
# Map
gph=ggplot(nodes_cluster%>%
inner_join(shp,by=c('mun')),aes(text=name_muni))+
geom_sf(aes(geometry=geom,fill=label),lwd=0.1,show.legend=F)+
geom_sf(data=shp_centroid%>%filter(name_muni=='São Paulo')%>%ungroup,aes(geometry=centr))+
theme_void()+
theme(axis.line=element_blank())+
scale_fill_manual(values=rep(RColorBrewer::brewer.pal(n=12,name='Paired'),times=10))+
guides(fill=guide_legend(ncol=3))+
labs(caption='Note: Check cluster by hovering over each municipality')
plotly::ggplotly(gph,tooltip=c('text','fill'))